package com.winai.launcher.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.winai.launcher.R;

/**
 * Created by 12985 on 2016/11/24.
 */

public class DialogUtils {

    public static Dialog dialog(Context context, View view, int animationStyle, int width, int gravity, Boolean isTouchOutside) {
        Dialog dialog = new Dialog(context, R.style.dialogThemeFullScr);
        dialog.setCanceledOnTouchOutside(isTouchOutside);
        dialog.setCancelable(isTouchOutside);
        Window window = dialog.getWindow();
//        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        WindowManager.LayoutParams wl = window.getAttributes();

        wl.gravity = gravity;
        Rect rect = new Rect();
        View v = window.getDecorView();
        v.getWindowVisibleDisplayFrame(rect);
        wl.width = width;
        if (animationStyle != 0)
            window.setWindowAnimations(animationStyle); //设置窗口弹出动画
        window.setAttributes(wl);
        dialog.setContentView(view);
        return dialog;
    }


    public static Dialog dialogCenter(Context context, View view, int width, boolean isOutSide) {
        Dialog dialog = new Dialog(context, R.style.dialogThemeFullScr);
        dialog.setCanceledOnTouchOutside(isOutSide);
        dialog.setCancelable(isOutSide);
        Window window = dialog.getWindow();
//        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        WindowManager.LayoutParams wl = window.getAttributes();

        wl.gravity = Gravity.CENTER;
        Rect rect = new Rect();
        View v = window.getDecorView();
        v.getWindowVisibleDisplayFrame(rect);
        wl.width = width;
        window.setAttributes(wl);
        dialog.setContentView(view);
        return dialog;
    }

    public static Dialog dialogBottom(Context context, View view, boolean isOutSide) {
        Dialog dialog = new Dialog(context, R.style.dialogThemeFullScr);
        dialog.setCanceledOnTouchOutside(isOutSide);
        dialog.setCancelable(isOutSide);
        Window window = dialog.getWindow();
//        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        WindowManager.LayoutParams wl = window.getAttributes();

        wl.gravity = Gravity.BOTTOM;
        Rect rect = new Rect();
        View v = window.getDecorView();
        v.getWindowVisibleDisplayFrame(rect);
        wl.width = ScreenUtils.getScreenWidth(context);
        window.setWindowAnimations(R.style.dialogAnimBottom);
        window.setAttributes(wl);
        dialog.setContentView(view);
        return dialog;
    }

    public static Dialog dialogBottom(Context context, final OnDialogResult onDialogResult) {
        View view = View.inflate(context, R.layout.dialog_result_bottom, null);
        ListView lvD = (ListView) view.findViewById(R.id.lv);
        TextView tvDCanncel = (TextView) view.findViewById(R.id.tv_d_canncel);

        final Dialog dialog = dialogBottom(context, view, true);
        tvDCanncel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        lvD.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onDialogResult != null) {
                    onDialogResult.onDialogOk(position);
                }
                dialog.dismiss();
            }
        });

        return dialog;
    }

    public interface OnDialogResult {
        void onDialogOk(int position);
    }
}
